package com.nanhai.buddhist.college.module.system.controller.admin.rolegroup;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupCreateReqVO;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupExcelVO;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupExportReqVO;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupPageReqVO;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupRespVO;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.RoleGroupUpdateReqVO;
import com.nanhai.buddhist.college.module.system.convert.rolegroup.RoleGroupConvert;
import com.nanhai.buddhist.college.module.system.dal.dataobject.rolegroup.RoleGroupDO;
import com.nanhai.buddhist.college.module.system.dal.dataobject.rolegroup.RoleGroupUserDO;
import com.nanhai.buddhist.college.module.system.service.rolegroup.RoleGroupService;
import com.nanhai.buddhist.college.module.system.service.rolegroup.RoleGroupUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 群组管理")
@RestController
@RequestMapping("/system/role-group")
@Validated
public class RoleGroupController {

    @Resource
    private RoleGroupService roleGroupService;


    @PostMapping("/create")
    @ApiOperation("创建群组管理")
    @PreAuthorize("@ss.hasPermission('system:role-group:create')")
    public CommonResult<Long> createRoleGroup(@Valid @RequestBody RoleGroupCreateReqVO createReqVO) {
        return success(roleGroupService.createRoleGroup(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新群组管理")
    @PreAuthorize("@ss.hasPermission('system:role-group:update')")
    public CommonResult<Boolean> updateRoleGroup(@Valid @RequestBody RoleGroupUpdateReqVO updateReqVO) {
        roleGroupService.updateRoleGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除群组管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:role-group:delete')")
    public CommonResult<Boolean> deleteRoleGroup(@RequestParam("id") Long id) {
        roleGroupService.deleteRoleGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得群组管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:role-group:query')")
    public CommonResult<RoleGroupRespVO> getRoleGroup(@RequestParam("id") Long id) {
        RoleGroupDO roleGroup = roleGroupService.getRoleGroup(id);
        return success(roleGroupService.convertRoleGroupRespVO(roleGroup));
    }

    @GetMapping("/list")
    @ApiOperation("获得群组管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:role-group:query')")
    public CommonResult<List<RoleGroupRespVO>> getRoleGroupList(@RequestParam("ids") Collection<Long> ids) {
        List<RoleGroupDO> list = roleGroupService.getRoleGroupList(ids);
        return success(RoleGroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得群组管理分页")
    @PreAuthorize("@ss.hasPermission('system:role-group:query')")
    public CommonResult<PageResult<RoleGroupRespVO>> getRoleGroupPage(@Valid RoleGroupPageReqVO pageVO) {
        PageResult<RoleGroupDO> pageResult = roleGroupService.getRoleGroupPage(pageVO);
        return success(RoleGroupConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出群组管理 Excel")
    @PreAuthorize("@ss.hasPermission('system:role-group:export')")
    @OperateLog(type = EXPORT)
    public void exportRoleGroupExcel(@Valid RoleGroupExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<RoleGroupDO> list = roleGroupService.getRoleGroupList(exportReqVO);
        // 导出 Excel
        List<RoleGroupExcelVO> datas = RoleGroupConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "群组管理.xls", "数据", RoleGroupExcelVO.class, datas);
    }

}
